/*
 * Copyright (c) OpenKoala 2011 All Rights Reserved
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.openkoala.koala.dd.event;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.actions.ActionGroup;
import org.openkoala.koala.dd.pojo.DDTree;
import org.openkoala.koala.dd.pojo.DDType;

/**
 * 类    名：DDTreeActionGroup.java
 *   
 * 功能描述：数据字典的右键菜单
 *  
 * 创建日期：2013-2-1上午9:45:03     
 * 
 * 版本信息：
 * 
 * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
 * 
 * 作    者：lingen(lingen.liu@gmail.com)
 * 
 * 修改记录： 
 * 修 改 者    修改日期     文件版本   修改说明	
 */
public class DDTreeActionGroup extends ActionGroup {
    
    private TreeViewer tv;

    private DDTreeAddAction addAction = new DDTreeAddAction();
    
    private DDTreeModifyAction deleteAction = new DDTreeModifyAction();
    /**
     * @param tv
     */
    public DDTreeActionGroup(TreeViewer tv) {
        super();
        this.tv = tv;
    }
    
    @Override
    public void fillContextMenu(IMenuManager mgr) {
        MenuManager menuManager = (MenuManager) mgr; // 类型转换
        Tree tree = tv.getTree();
        Menu menu = menuManager.createContextMenu(tree);
        tree.setMenu(menu);
        mgr.addMenuListener(new IMenuListener() {

            @Override
            public void menuAboutToShow(IMenuManager manager) {
                manager.removeAll();
                TreeItem items[] = tv.getTree().getSelection();
                DDTree tree = (DDTree)items[0].getData();
                if(tree.getType().equals(DDType.static_common) || tree.getType().equals(DDType.query_common)){
                    manager.add(deleteAction);
                }else if(tree.getType().equals(DDType.staticDropDown) || tree.getType().equals(DDType.queryDropDown)){
                    manager.add(addAction);
                }
            }
            
        });
    }
    
    
    
    /**
     * 
     * 类    名：DDTreeActionGroup.java
     *   
     * 功能描述：新增数据字典	
     *  
     * 创建日期：2013-2-1上午9:49:21     
     * 
     * 版本信息：
     * 
     * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
     * 
     * 作    者：lingen(lingen.liu@gmail.com)
     * 
     * 修改记录： 
     * 修 改 者    修改日期     文件版本   修改说明
     */
    private class DDTreeAddAction extends Action {
        public DDTreeAddAction() {
            setText("新增数据字典");
        }

        public void run() {
            InputDialog input = new InputDialog(Display.getCurrent().getActiveShell(),"新增数据字典","输入数据字典的名称:","",null);
            input.open();
            int returnValue = input.getReturnCode();
            if(returnValue==0){
                //新增一个DDtree
                TreeItem items[] = tv.getTree().getSelection();
                DDTree parent = (DDTree)items[0].getData();
                DDTree add = null;
                if(parent.getType().equals(DDType.staticDropDown)){
                    add = DDTree.getStaticCommonDDTree(input.getValue(),parent);
                }
                if(parent.getType().equals(DDType.queryDropDown)){
                    add = DDTree.getQueryCommonDDTree(input.getValue(),parent);
                }
                parent.getChildren().add(add);
                tv.refresh();
                tv.expandAll();
            }
        }
    }
    
    /**
     * 
     * 类    名：DDTreeActionGroup.java
     *   
     * 功能描述：删除数据字典	
     *  
     * 创建日期：2013-2-1上午9:49:32     
     * 
     * 版本信息：
     * 
     * 版权信息：Copyright (c) 2013 Koala All Rights Reserved
     * 
     * 作    者：lingen(lingen.liu@gmail.com)
     * 
     * 修改记录： 
     * 修 改 者    修改日期     文件版本   修改说明
     */
    private class DDTreeModifyAction extends Action{
        
        public DDTreeModifyAction(){
            setText("删除数据字典");
        }
        
        public void run(){
            TreeItem items[] = tv.getTree().getSelection();
            DDTree child = (DDTree)items[0].getData();
            child.getParent().getChildren().remove(child);
            tv.refresh();
            tv.expandAll();
        }
    }
}
